package com.example.chatapp.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/health")
public class HealthController {
    
    private final RedisTemplate<String, Object> redisTemplate;
    
    public HealthController(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    
    @GetMapping("/redis")
    public Map<String, Object> checkRedis() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 测试 Redis 连接
            String testKey = "health:test";
            redisTemplate.opsForValue().set(testKey, "OK");
            String value = (String) redisTemplate.opsForValue().get(testKey);
            redisTemplate.delete(testKey);
            
            result.put("status", "OK");
            result.put("message", "Redis connection is working");
            result.put("testValue", value);
        } catch (Exception e) {
            result.put("status", "ERROR");
            result.put("message", "Redis connection failed: " + e.getMessage());
            result.put("error", e.getClass().getName());
        }
        return result;
    }
    
    @GetMapping("/")
    public Map<String, String> health() {
        Map<String, String> result = new HashMap<>();
        result.put("status", "UP");
        result.put("application", "WebSocketChat");
        return result;
    }
}
